
package com.idealighter.game.games.happyfive.handler;

import com.google.common.collect.Lists;

import com.idealighter.game.core.annotation.ResMsgHandler;
import com.idealighter.game.gamehall.dto.SeatInfo;
import com.idealighter.game.games.happyfive.HappyFivePlayer;
import com.idealighter.game.games.happyfive.dto.PlayerCards;
import com.idealighter.game.games.happyfive.message.ReqCheckCardMsg;
import com.idealighter.game.games.happyfive.message.ReqDealCardOverMsg;
import com.idealighter.game.games.happyfive.message.ResOpenHandInfoMsg;
import com.idealighter.game.message.core.ResMessage;
import com.idealighter.game.robot.core.Player;
import com.idealighter.game.robot.handler.ResMessageHandler;
import com.idealighter.utils.code.RandCodeUtil;

import java.util.List;
import java.util.concurrent.TimeUnit;


@ResMsgHandler(507227)
public class ResOpenHandInfoHandler implements ResMessageHandler {

  // private static final Logger LOG = LoggerFactory.getLogger(ResOpenHandInfoHandler.class);

  @Override
  public void action(Player player, ResMessage message) {

    HappyFivePlayer happyFivePlayer = (HappyFivePlayer) player;
    SeatInfo seatInfo = happyFivePlayer.seat(player.playerId);
    happyFivePlayer.betCount = 1;
    happyFivePlayer.curBet = 0;
    happyFivePlayer.isRaise = false;
    ResOpenHandInfoMsg msg = (ResOpenHandInfoMsg) message;
    for (PlayerCards cards : msg.getCards()) {
      List<Integer> cardList = Lists.newArrayList();
      cardList.addAll(cards.getCards());
      if (cards.getOrder() == seatInfo.getOrder()) {
        // gameStrategy.cardList.addAll(cardList);
        isContains(cardList, happyFivePlayer.cardList);
        continue;
      }
      if (happyFivePlayer.othersCardList.containsKey(cards.getOrder())) {
        happyFivePlayer.othersCardList.get(cards.getOrder()).addAll(cardList);
      } else {
        happyFivePlayer.othersCardList.put(cards.getOrder(), cardList);
      }
    }
    player.schedule(() -> player.sendMsg(new ReqDealCardOverMsg()), RandCodeUtil.random(3, 5),
        TimeUnit.SECONDS);
    if (RandCodeUtil.randomBoolean()) {
      player.sendMsg(new ReqCheckCardMsg());
    }
  }


  /**
   * 判断包含.
   *
   * @param msgList .
   * @param cardList .
   */
  private void isContains(List<Integer> msgList, List<Integer> cardList) {

    msgList.stream().filter(card -> !cardList.contains(card)).forEach(cardList::add);

    // for (Integer card : cardList) {
    // cardList.addAll(msgList.stream().filter(msgCard -> card !=
    // msgCard).collect(Collectors.toList()));
    // }
  }
}
